package com.demo.meidianapp.mvp.ui.activity;

import android.Manifest;
import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.FragmentTransaction;
import android.util.Log;

import com.ashokvarma.bottomnavigation.BottomNavigationBar;
import com.ashokvarma.bottomnavigation.BottomNavigationItem;
import com.demo.meidianapp.R;
import com.demo.meidianapp.mvp.base.baseImp.BaseActivity;
import com.demo.meidianapp.mvp.base.baseImp.BaseFragment;
import com.demo.meidianapp.mvp.base.baseImp.BasePresenter;
import com.tbruyelle.rxpermissions2.RxPermissions;
import com.youth.banner.Banner;

import java.util.ArrayList;

import butterknife.BindView;
import io.reactivex.functions.Consumer;


public class HomeActivity extends BaseActivity implements BottomNavigationBar.OnTabSelectedListener {

    @BindView(R.id.bottom_navigation_bar)
    BottomNavigationBar bottom_navigation_bar;
    ArrayList<BaseFragment> fragments = new ArrayList<>();
    Banner banner;

    @SuppressLint("CheckResult")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initFragment();
        initView();
        RxPermissions rxPermissions = new RxPermissions(this);
        rxPermissions.request(Manifest.permission.CAMERA,
                Manifest.permission.READ_PHONE_STATE).subscribe(new Consumer<Boolean>() {
            @Override
            public void accept(Boolean aBoolean) throws Exception {
                if (aBoolean) {
                    // All requested permissions are granted
                } else {
                    // At least one permission is denied
                }

            }
        });

    }

    @Override
    public int initView(@Nullable Bundle savedInstanceState) {
        return R.layout.home_activity;
    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
    }


    public void initFragment() {
//        myFragment = new MyFragment();
//        publishFragment = new PublishFragment();
//        welfareFragment = new WelfareFragment();
//        homeFragment = new HomeFragment();
//        fragments.add(publishFragment);
//        fragments.add(welfareFragment);
//        fragments.add(homeFragment);
//        fragments.add(myFragment);

    }

    public void initView() {
        bottom_navigation_bar
                .setMode(BottomNavigationBar.MODE_FIXED) // 设置mode
                .setBackgroundStyle(BottomNavigationBar.BACKGROUND_STYLE_RIPPLE)  // 背景样式
                .setBarBackgroundColor("#2FA8E1") // 背景颜色
                .setInActiveColor("#929292") // 未选中状态颜色
                .setActiveColor("#ffffff") // 选中状态颜色
                .addItem(new BottomNavigationItem(R.drawable.publish, "消息").setInactiveIconResource(R.drawable.publishno)) // 添加Item
                .addItem(new BottomNavigationItem(R.drawable.welfare, "福利").setInactiveIconResource(R.drawable.weifareno))
                .addItem(new BottomNavigationItem(R.drawable.addpng, "发布").setInactiveIconResource(R.drawable.addpng))
                .addItem(new BottomNavigationItem(R.drawable.home, "首頁").setInactiveIconResource(R.drawable.homeno))
                .addItem(new BottomNavigationItem(R.drawable.my, "我的").setInactiveIconResource(R.drawable.myno))
                .setFirstSelectedPosition(2) //设置默认选中位置
                .initialise();  // 提交初始化（完成配置）
        bottom_navigation_bar.setTabSelectedListener(this);
//        FrameLayout mFrameLayout = (FrameLayout) bottom_navigation_bar.findViewById(com.ashokvarma.bottomnavigation.R.id.bottom_n
// avigation_bar_container);//得到
//         mFrameLayout.setBackgroundResource(R.drawable.home);//id:背景图id

//        getSupportFragmentManager().beginTransaction().replace(R.id.home_activity_frag_container, homeFragment).commitAllowingStateLoss();
        showFragment(2);
    }


    public void showFragment(int position) {
//        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
//        if (!fragments.get(position).isAdded()) {
//            transaction.add(R.id.home_activity_frag_container, fragments.get(position));
//        }
//        hideFragment(transaction);
//        transaction.show(fragments.get(position));
//        transaction.commit();
    }

    @Override
    public void onTabSelected(int position) {
        setScrollableText(position);
    }

    @Override
    public void onTabUnselected(int position) {

    }

    @Override
    public void onTabReselected(int position) {

    }

    //隐藏所有的fragment
    private void hideFragment(FragmentTransaction transaction) {
//        for (int i = 0; i < fragments.size(); i++) {
//            transaction.hide(fragments.get(i));
//        }
    }


    private void setScrollableText(int position) {
        if (position == 2) {
            return;
        }
        if (position > 2) {
            position = position - 1;
        }
        showFragment(position);
//        switch (position) {
//            case 0:
//                getSupportFragmentManager().beginTransaction().replace(R.id.home_activity_frag_container, publishFragment).commitAllowingStateLoss();
//                break;
//            case 1:
//                getSupportFragmentManager().beginTransaction().replace(R.id.home_activity_frag_container, welfareFragment).commitAllowingStateLoss();
//                break;
//            case 2:
//                getSupportFragmentManager().beginTransaction().replace(R.id.home_activity_frag_container, homeFragment).commitAllowingStateLoss();
//                break;
//            case 3:
//                getSupportFragmentManager().beginTransaction().replace(R.id.home_activity_frag_container, myFragment).commitAllowingStateLoss();
//                break;
//            default:
//                getSupportFragmentManager().beginTransaction().replace(R.id.home_activity_frag_container, homeFragment).commitAllowingStateLoss();
//                break;
//        }
    }

    @Override
    public BasePresenter initPresenter() {
        return null;
    }
}
